package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.dto.ProductDTO;
import com.wanxi.service.ProductService;
import com.wanxi.service.impl.ProductServiceImpl;
import com.wanxi.util.Tool;
import com.wanxi.vo.ProductVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //        1.乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        2.拿到前端的值
        String whereShow = req.getParameter("whereShow");
        String cuisineId = req.getParameter("cuisineId");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");


//        3.封装VO
        ProductVo productVo = new ProductVo();
        productVo.setWhereShow(Tool.strToInt(whereShow));
        productVo.setCuisineId(Tool.strToInt(cuisineId));
        productVo.setPage(Tool.strToPage(page));
        productVo.setLimit(Tool.strToLimit(limit));
        //        4.调用service
        ProductService productService = new ProductServiceImpl();
        //        5.得到返回值
        List<ProductDTO> productDTOList = productService.findAllProduct(productVo);
        //        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(productDTOList));

    }
}
